package com.zz.demo.thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class MyReentrantLock implements Runnable {
    private Lock lock = new ReentrantLock();
    @Override
    public void run() {
        //上锁
        lock.lock();
        for (int i = 0; i < 5;i ++) {
            System.out.println("当前线程名： " + Thread.currentThread().getName() + ", i = " + i);
        }
        //释放锁
        lock.unlock();
    }
}
public class MyLock {
    public static void main(String[] args) {
        MyReentrantLock myReentrantLock = new MyReentrantLock();
        Thread thread1 = new Thread(myReentrantLock);
        Thread thread2 = new Thread(myReentrantLock);
        Thread thread3 = new Thread(myReentrantLock);

        thread3.start();
        thread1.start();
        thread2.start();
    }
}


